﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnguaRPGEngine.Library.Systems;
using AnguaRPGEngine.Library.Parts;
using AnguaRPGEngine.Library.Forms;
using log4net;
using System.Reflection;
using AnguaRPGEngine.Library.Interfaces;

namespace AnguaRPGEngine.MapEditor
{
    public partial class MapEditorForm : Form
    {
        private static readonly ILog _Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private List<Form> _Windows = new List<Form>();
        public MapEditorForm()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                InitializeComponent();
                ImageLibrary.Instance.LoadImageLibrary(Properties.Settings.Default.PathToImages);

            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }

        private void MapEditorForm_Load(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                using (SplashScreen ss = new SplashScreen(string.Format("Map Editor v{0}", Application.ProductVersion)))
                {
                    ss.ShowDialog();
                }
                AddNewMap();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }

        private void tsBtnNewMap_Click(object sender, EventArgs e)
        {
            AddNewMap();
        }
        private void AddNewMap()
        {
            AddNewMap(string.Empty, null, string.Empty);
        }
        private void AddNewMap(string mapName, List<IPart> map, string mapFileName)
        {
            MapContainer mapForm = new MapContainer();
            if (mapName == string.Empty)
            {
                mapForm.Text = "Map" + _Windows.Count.ToString().PadLeft(5, '0');
                mapForm.Map = new List<IPart>();
                mapForm.MapFileName = "";
                mapForm.MapName = mapForm.Text;
            }
            else
            {
                mapForm.Text = mapName;
                mapForm.MapFileName = mapFileName;
                mapForm.MapName = mapName;
                mapForm.Map = map;
            }
            mapForm.MdiParent = this;
            mapForm.WindowState = FormWindowState.Maximized;
            mapForm.FormClosed += new FormClosedEventHandler(mapForm_FormClosed);
            mapForm.Show();
            _Windows.Add(mapForm);
            ToolStripMenuItem tsmi = new ToolStripMenuItem();
            tsmi.Name = mapForm.Name;
            tsmi.Text = tsmi.Name;
            tsmi.Click += new EventHandler(tsmi_Click);
            tsBtnWindows.DropDownItems.Add(tsmi);
        }

        void mapForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            _Windows.Remove((Form)sender);
        }

        void tsmi_Click(object sender, EventArgs e)
        {
            _Windows.Find(p => p.Name == ((ToolStripMenuItem)sender).Text).BringToFront();
        }

        private void tsBtnOpenMap_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Title = "Open Map";
                ofd.Filter = "Angua R.P.G. Engine Maps (*.rpgmap)|*.rpgmap";
                ofd.AddExtension = true;
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    List<IPart> map = new List<IPart>();
                    MapFileClass mapFileClass = Serializer<MapFileClass>.DeserializeFromFile(ofd.FileName);
                    foreach (Floor floor in mapFileClass.Floors)
                    {
                        map.Add(floor);
                    }
                    foreach (Door door in mapFileClass.Doors)
                    {
                        map.Add(door);
                    }
                    foreach (Scenery scenery in mapFileClass.Scenery)
                    {
                        map.Add(scenery);
                    }
                    foreach (Counter counter in mapFileClass.Counters)
                    {
                        map.Add(counter);
                    }
                    foreach (Effect effect in mapFileClass.Effects)
                    {
                        map.Add(effect);
                    }
                    foreach (Wall wall in mapFileClass.Walls)
                    {
                        map.Add(wall);
                    }
                    foreach (Markup markup in mapFileClass.Markups)
                    {
                        map.Add(markup);
                    }
                    if (map != null)
                    {
                        string MapName = ofd.FileName.Remove(0, ofd.FileName.LastIndexOf("\\") + 1);
                        MapName = MapName.Substring(0, MapName.LastIndexOf("."));
                        AddNewMap(MapName, map, ofd.FileName);
                    }
                }
            }
        }

        private void tsBtnSaveMap_Click(object sender, EventArgs e)
        {
            if (((MapContainer)this.ActiveMdiChild).MapFileName != "")
            {
                ((MapContainer)this.ActiveMdiChild).SaveMap();
            }
            else
            {
                tsBtnSaveMapAs_Click(sender, e);
            }
        }

        private void tsBtnSaveMapAs_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.Title = "Save Map As...";
                sfd.Filter = "Angua R.P.G. Engine Maps (*.rpgmap)|*.rpgmap";
                sfd.AddExtension = true;
                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {

                    string MapName = sfd.FileName.Remove(0, sfd.FileName.LastIndexOf("\\") + 1);
                    MapName = MapName.Substring(0, MapName.LastIndexOf("."));
                    ((MapContainer)this.ActiveMdiChild).MapName = MapName;
                    ((MapContainer)this.ActiveMdiChild).MapFileName = sfd.FileName;
                    tsBtnSaveMap_Click(sender, e);
                }
            }
        }

        private void aboutAnguaRPGMapEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (AboutForm frmAbout = new AboutForm(this.Text))
            {
                frmAbout.ShowDialog();
            }
        }

        private void MapEditorForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.partsControl1.ClearSelection();
            }
        }

       
    }
}